using System;
using System.Collections;
using System.Threading;
using FlowCanvas;
using FlowCanvas.Nodes;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    public class PromiseRequestCompleteCityAreaEvent : FlowControlNode
    {
        public BBParameter<int> eventId;
        public BBParameter<float> retryInterval = 0.5f;
        private FlowOutput caseThen;

        public IEnumerator Invoke(Flow f)
        {
            yield return new WaitForOneFrame();
            yield return new WaitForOneFrame();
            BlueprintUtils.RequestCompleteAreaEvent(eventId.value);
            yield return new WaitForSeconds(retryInterval.value);
            
            while (!BlueprintUtils.HasFinishCityEvent(eventId.value))
            {
                BlueprintUtils.RequestCompleteAreaEvent(eventId.value);
                yield return new WaitForSeconds(retryInterval.value);
            }
            
            if(caseThen != null)
                f.Call(caseThen);
        }

        protected override void RegisterPorts()
        {
            caseThen = AddFlowOutput("Then");
            AddFlowInput("In", (f) =>
            {
                StartCoroutine(Invoke(f));
            });
        }
    }
}